//
//  SANewSaleDocumentsDetailCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/3/23.
//

#import "SANewSaleDocumentsDetailCell.h"
#import "SASalesInvoiceDetailModel.h"
#import "SASalesOrderViewListModel.h"

@interface SANewSaleDocumentsDetailCell ()

@property (nonatomic, strong) UILabel *colourSizeLab;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *discountPriceLab;
@property (nonatomic, strong) UILabel *percentageLab;
@property (nonatomic, strong) UILabel *totalPriceLab;

@property (nonatomic, strong) UILabel *returnLab;
@property (nonatomic, strong) UIView *line;

@end

@implementation SANewSaleDocumentsDetailCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASaleSkuListModel *)model {
    _model = model;

    self.colourSizeLab.text = [NSString stringWithFormat:@"%@,%@",model.colourName,model.sizeName];
    NSString *price = [NSString stringWithFormat:@"%0.2f",[model.price floatValue]/100.f].getEffectivePointString;
    self.priceLab.text = [NSString stringWithFormat:@"单价:%@*%@",price,model.count];
    
    CGFloat discountRate = [model.discountRate floatValue]/100;
    if (discountRate == 100) {
        self.totalPriceLab.frame = SARectMake(211, 1, 75, 54);
        self.percentageLab.text = @"";
        NSString *textContent = [NSString stringWithFormat:@"%0.2f",[model.totalPrice floatValue]/100.f].getEffectivePointString;
        self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:nil];
    }else {
        NSString *percentageStr = [NSString stringWithFormat:@"%0.2f",discountRate].getEffectivePointString;
        self.percentageLab.text = [NSString stringWithFormat:@"%@%%",percentageStr];
        self.totalPriceLab.frame = SARectMake(211, 28, 75, 20);
        NSString *textContent = [NSString stringWithFormat:@"%0.2f",[model.totalPrice floatValue]/100.f].getEffectivePointString;
        NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
        textDict[NSStrikethroughColorAttributeName] = kPinkColor;
        textDict[NSForegroundColorAttributeName] = kBlackColor;
        textDict[NSFontAttributeName] = kFont(14);
        textDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
        textDict[NSBaselineOffsetAttributeName] = @(NSUnderlineStyleSingle);
        self.totalPriceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
    }
    
    self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2f",[model.discountPrice floatValue]/100.f].getEffectivePointString;
    if ([model.returnCount isEqualToString:@"1"]) {
        self.returnLab.hidden = NO;
        self.discountPriceLab.frame = SARectMake(287, 8, 88, 20);
    }else {
        self.returnLab.hidden = YES;
        self.discountPriceLab.frame = SARectMake(287, 1, 88, 54);
    }
    
    if ([model.discountPrice floatValue] < 0) {
        self.discountPriceLab.textColor = kPinkColor;
    }else {
        self.discountPriceLab.textColor = kBlackColor;
    }
}

-(void)setEnterModel:(SASalesOrderToPendSkuList *)enterModel {
    _enterModel = enterModel;
    self.colourSizeLab.frame = SARectMake(80, 8, 200, 20);
    self.priceLab.frame = SARectMake(80, 28, 200, 20);
    self.returnLab.hidden = YES;

    self.colourSizeLab.text = [NSString stringWithFormat:@"%@,%@",enterModel.colourName,enterModel.sizeName];
    NSString *price = [NSString stringWithFormat:@"%0.2f",[enterModel.price floatValue]/100.f].getEffectivePointString;
    self.priceLab.text = [NSString stringWithFormat:@"单价:%@,数量:%@",price,enterModel.count];
    self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2lf",[enterModel.subtotal doubleValue]/100.f].getEffectivePointString;
    
    if ([enterModel.subtotal floatValue] < 0) {
        self.discountPriceLab.textColor = kPinkColor;
    }else {
        self.discountPriceLab.textColor = kBlackColor;
    }
}

-(void)setInventoryModel:(SASalesOrderToPendSkuList *)inventoryModel {
    _inventoryModel = inventoryModel;
    self.colourSizeLab.frame = SARectMake(80, 8, 200, 20);
    self.priceLab.frame = SARectMake(80, 28, 200, 20);
    self.returnLab.hidden = YES;

    self.colourSizeLab.text = [NSString stringWithFormat:@"%@,%@",inventoryModel.colourName,inventoryModel.sizeName];
    self.priceLab.text = [NSString stringWithFormat:@"盘前:%@,盘后:%@",inventoryModel.count,inventoryModel.stock];
    self.discountPriceLab.text = inventoryModel.subtotal;
}

-(void)initUI {
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.colourSizeLab];
    [self.contentView addSubview:self.numLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.discountPriceLab];
    [self.contentView addSubview:self.percentageLab];
    [self.contentView addSubview:self.totalPriceLab];
    [self.contentView addSubview:self.returnLab];
}

-(UILabel *)colourSizeLab {
    if (!_colourSizeLab) {
        _colourSizeLab = [[UILabel alloc] initWithFrame:SARectMake(80, 8, 130, 20)];
        _colourSizeLab.textColor = kLightgGrayColor;
        _colourSizeLab.font = kFont(14);
    }
    return _colourSizeLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(80, 28, 130, 20)];
        _priceLab.textColor = kLightgGrayColor;
        _priceLab.font = kFont(14);
        _priceLab.adjustsFontSizeToFitWidth = YES;
        _priceLab.minimumScaleFactor = 0.8;
    }
    return _priceLab;
}

-(UILabel *)percentageLab {
    if (!_percentageLab) {
        _percentageLab = [[UILabel alloc] initWithFrame:SARectMake(211, 8, 75, 20)];
        _percentageLab.textColor = kPinkColor;
        _percentageLab.font = kFont(14);
        _percentageLab.textAlignment = NSTextAlignmentCenter;
    }
    return _percentageLab;
}

-(UILabel *)totalPriceLab {
    if (!_totalPriceLab) {
        _totalPriceLab = [[UILabel alloc] initWithFrame:SARectMake(211, 1, 75, 54)];
        _totalPriceLab.textColor = kBlackColor;
        _totalPriceLab.font = kFont(14);
        _totalPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _totalPriceLab;
}

-(UILabel *)discountPriceLab {
    if (!_discountPriceLab) {
        _discountPriceLab = [[UILabel alloc] initWithFrame:SARectMake(287, 1, 88, 54)];
        _discountPriceLab.textColor = kBlackColor;
        _discountPriceLab.font = kFont(14);
        _discountPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _discountPriceLab;
}

-(UILabel *)returnLab {
    if (!_returnLab) {
        _returnLab = [[UILabel alloc] initWithFrame:SARectMake(287, 28, 88, 20)];
        _returnLab.textColor = kPinkColor;
        _returnLab.font = kFont(13);
        _returnLab.textAlignment = NSTextAlignmentCenter;
        _returnLab.text = @"已退款";
    }
    return _returnLab;
}

-(UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] initWithFrame:SARectMake(80, 0, kBaseScreenWidth - 80, 1)];
        _line.backgroundColor = kLineColor;
    }
    return _line;
}

@end
